﻿using System;
using Kaleida.ServiceMonitor.Model;
using NUnit.Framework;

namespace Kaleida.UnitTests.Model
{
    [TestFixture]
    public class CodePlexWebVersionCheckTests
    {
        [Test]
        public void TestParseTitle()
        {
            const string html = "blah blah <h1 class=\"page_title\"><a href=\"#\">ServiceMon Release 0.9.0.44</a></h1>blah blah ";
            Assert.AreEqual("0.9.0.44", CodePlexWebVersionCheck.ExtractVersionFromHtmlPage(html).ToString());
        }

        [Test]
        public void TestCanParseVersionWhenPageContainsMultipleReleases()
        {
            const string html = "blah blah <h1 class=\"page_title\"><a href=\"#\">ServiceMon Release 1.0.0.106</a></h1>blah blah " +
                                "blah blah <h1 class=\"page_title\"><a href=\"#\">ServiceMon Release 0.9.0.44</a></h1>blah blah ";
            Assert.AreEqual("0.9.0.44", CodePlexWebVersionCheck.ExtractVersionFromHtmlPage(html).ToString());
        }

        [Test, ExpectedException(typeof(InvalidOperationException), ExpectedMessage = "Couldn't extract latest version from CodePlex page. Unexpected format")]
        public void TestExceptionIsThrownIfUnexpectedPageFormat()
        {
            CodePlexWebVersionCheck.ExtractVersionFromHtmlPage("<h1 class=\"pagetitle\">ServiceMon Release 0.9.0.44</h1>");
        }

        [Test, ExpectedException(typeof(InvalidOperationException), ExpectedMessage = "Couldn't extract latest version from CodePlex page. Unexpected format")]
        public void TestExceptionIsThrownIfUnexpectedVersionFormat()
        {
            CodePlexWebVersionCheck.ExtractVersionFromHtmlPage("<h1 class=\"pagetitle\">ServiceMon Release 0.90.44</h1>");
        }
    }
}
